#version 330 core
//材质
struct Material {
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
}; 

//光源颜色
struct Light {
    vec3 direction; //光源放向

    vec3 ambient;  //环境光颜色
    vec3 diffuse;  //漫反射光源颜色
    vec3 specular; //镜面反射光源颜色
};

out vec4 FragColor;

in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoords;

uniform vec3 viewPos;
uniform Material material;
uniform Light light;

void main()
{
    // ambient
    vec3  ambient = light.ambient * texture(material.diffuse, TexCoords).rgb;                           //计算后的环境光颜色

    // diffuse
    vec3  norm = normalize(Normal);
    vec3  lightDir = normalize(-light.direction);                       //漫反射方向
    float diff = max(dot(norm, lightDir), 0.0);                                 //漫反射强度
    vec3  diffuse = light.diffuse * diff * texture(material.diffuse, TexCoords).rgb;                 //计算后的漫反射灯光颜色

    // specular
    vec3  viewDir = normalize(viewPos - FragPos);                               //视线方向
    vec3  reflectDir = reflect(-lightDir, norm);                                //反射光方向
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);   //镜面反射强度(这个32是高光的反光度(Shininess))
    vec3  specular = light.specular * spec * texture(material.specular, TexCoords).rgb;               //计算后的镜面反色颜色

    vec3 result = ambient + diffuse + specular;                                 //计算后的物体实际颜色
    FragColor = vec4(result, 1.0);
}